package com.skyhub.controller;

import cn.hutool.json.JSONUtil;
import com.skyhub.common.domain.dto.UserDTO;
import com.skyhub.common.domain.pojo.Result;
import com.skyhub.common.domain.pojo.User;
import com.skyhub.common.domain.vo.UserLoginVo;
import com.skyhub.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/username/login")
    public Result UsernameLogin(@RequestBody User user){

        log.info("用户名登录：{}",user);

        UserLoginVo userLoginVo=userService.loginForUsername(user);

        if(JSONUtil.isNull(userLoginVo.getUser())){
            return Result.error("登录错误", "用户名或密码错误");
        }

        return Result.success("用户名登录成功",userLoginVo);
    }
    @PostMapping("/email/login")
    public Result EmailLogin(@RequestBody User user){

        log.info("邮箱登录：{}",user);

        UserLoginVo userLoginVo=userService.loginForEmail(user);

        if(JSONUtil.isNull(userLoginVo.getUser())){
            return Result.error("登录错误","邮箱或密码错误");
        }

        return Result.success("邮箱登录成功",userLoginVo);
    }
    @PostMapping("/register/valiCode")
    public Result GetRegisterValidCode(@RequestBody User user){
        System.out.println("email"+user.getEmail());
        userService.getRegisterValidiCode(user);
        return Result.success("验证码获取成功,请转入163邮箱");
    }
    @PutMapping("/register")
    public Result Register(@RequestBody UserDTO userDTO){
        log.info("user-dto:{}",userDTO);
        Boolean flag=userService.Register(userDTO);
        if(flag){
            return Result.success("注册成功","如果你在平台乱发文章，我们就他妈的去杀了你全家");
        }else{
            return Result.error("注册失败","用户名或邮箱已存在");
        }
    }
    @PostMapping("/forgetPassword")
    public Result ForgetPassword(@RequestBody UserDTO userDTO){
        Boolean flag=userService.ForgetPassword(userDTO);
        if(!flag){
            return Result.error("密码找回失败","验证码错了,他妈的,别乱写!!!");
        }
        return Result.success("找回密码成功");
    }
}